import GameData_blxf from "../GameData/GameData";
import { ConfigModule_blxf } from "../Module/ConfigModule";
import PublicModule_blxf from "../Module/PublicModule";
import { UtilsModule_blxf } from "../Module/UtilsModule";

// 引导挪动商品，标记商品、售卖商品
let _guiList : any[] = [
    // { x: 0, y:   -570, width: 268.5, height: 106.5, isUpdateY: -1 },
    { x: -262.5, y: 126.5, width: 160, height: 225, isUpdateY: -3/4 },
    { x:  -87.5, y: 126.5, width: 160, height: 225, isUpdateY: -3/4 },
    { x:   87.5, y: 126.5, width: 160, height: 225, isUpdateY: -3/4 },
    { x: -262.5, y: 126.5, width: 160, height: 225, isUpdateY: -3/4 }
    // { x: -270, y: -357.5, width: 160, height: 225 },
    // { x: 87.5, y:    283, width: 150, height: 150 },
    
    // { x:  180, y: -570, width: 268.5, height: 106.5, isUpdateY: -1 },
]

// 引导低价收购
let _guiList_2 : any[] = [
    { x:    0, y: -545, width: 160, height: 225 },
    { x: -230, y:  400, width: 120, height: 160 },
]

// 自动售卖引导解锁装饰
let _guiList_3 : any[] = [
    { x:  270, y: -590, width: 200, height: 120, isUpdateY: -1 },
    { x: -210, y:  254, width: 120, height: 130 },
    { x:    0, y: -170, width: 255, height:  95 },
]

// 房间内部引导解锁装饰
let _guiList_4 : any[] = [
    { x:    0, y: -395, width: 241, height: 116, isUpdateY: -1 },
    { x: -210, y:  254, width: 120, height: 130 },
    { x:    0, y: -170, width: 255, height:  95 },
]

const {ccclass, property} = cc._decorator;

/** 新手教程 */

@ccclass
export default class Guidance_blxf extends cc.Component {

    @property({ type: cc.Node, tooltip: '遮罩背景' })
    maskBg: cc.Node = null;

    @property({ type: cc.Node, tooltip: '手指盒子' })
    handBox: cc.Node = null;

    @property({ type: cc.Node, tooltip: '手指动画节点' })
    handNode: cc.Node = null;

    @property({ type: cc.Node, tooltip: '禁止点击节点' })
    stopClickNode: cc.Node = null;


    @property({ tooltip: '传入参数' })
    _options: any = {};

    @property({ tooltip: '引导类型（1送货、2低价收购、3自动售卖装饰、4房间内部装饰）' })
    _guiType: number = null;

    @property({ tooltip: '引导数据列表' })
    _guiDataList: any[] = [];

    @property({ tooltip: '引导下标' })
    _guiIndex: number = 0;

    @property({ tooltip: '是否可以点击' })
    _isTouch: boolean = false;

    @property({ tooltip: '手指动画' })
    _handTween: cc.Tween = null;

    @property({ tooltip: '是否引导操作游戏' })
    _isGui: boolean = false;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        UtilsModule_blxf.instance.setNodeSize_blxf( this.node );
    }

    start () {
        this.initEvent_blxf();
    }

    initEvent_blxf () {
        this.handBox.on('touchstart', this.onTouchNext_blxf, this);
        this.handBox['_touchListener'].setSwallowTouches(false);
    }

    initData_blxf ( options: any ) {
        this._options = options;

        let { type } = this._options;
        this._guiType = type;
        this._guiIndex = 0;

        // this.onCheckUpdatePos_blxf();
        this.initDataList_blxf();

        let _t: number = this._guiType == 1 ? 1.5 : 0.01;
        this.scheduleOnce(()=>{
            this.onGuidance_blxf();
        }, _t);
    }

    initDataList_blxf () {
        let _dataList: any[] = [];
        switch ( this._guiType ) {
            case 1 : _dataList = _guiList; break;
            case 2 : _dataList = _guiList_2; break;
            case 3 : _dataList = _guiList_3; break;
            case 4 : _dataList = _guiList_4; break;
        }
        this._guiDataList = UtilsModule_blxf.instance.getCopyData_blxf( _dataList );
    }

    // update (dt) {}

    updateUI_blxf () {
        this.maskBg.width = 0;
        this.maskBg.height = 0;

        this.handBox.active = false;
        this.stopClickNode.active = true;

        this._handTween && this._handTween.stop();
        this._handTween = null;
    }

    onGuidance_blxf () {
        let _data = this._guiDataList[ this._guiIndex ];
        if ( !_data ) return this.onClose_blxf();

        this._isTouch = false;
        this.updateUI_blxf();
        this.onUpdateMask_blxf( _data );

        this._guiIndex += 1;
    }

    onUpdateMask_blxf ( _data: any ) {
        let _moveTime: number = 0.5;
        let { x, y, width, height, isUpdateY } = _data;
        
        let { checkH } = ConfigModule_blxf.instance;
        if ( isUpdateY ) y += checkH * isUpdateY;
        // if ( this._guiType == 1 ) {
        //     y += checkH * ( y > 0 ? -1/2 : -1 );
        // }

        this.handBox.x = x;
        this.handBox.y = y;
        cc.tween( this.maskBg )
            .to( _moveTime, { x, y, width, height })
            .call(()=>{
                this._isTouch = true;
                this.handBox.width = width;
                this.handBox.height = height;
                this.handBox.active = true;

                this.onHandAnimation_blxf();

                this.scheduleOnce(()=>{
                    if ( !this || !this.stopClickNode ) return;
                    this.stopClickNode.active = false;
                }, 0.2)
            }).start();
    }

    onTouchNext_blxf ( touch: cc.Event ) {
        if ( !this._isTouch ) return;

        this.stopClickNode.active = true;
        this.onGuidance_blxf();
    }

    onClose_blxf () {
        this.node.destroy();

        PublicModule_blxf.isGui = false;

        switch ( this._guiType ) {
            case 1 : GameData_blxf.instance.setIsGuidance_blxf(); break;
            case 2 : GameData_blxf.instance.setIsGuidanceAllShop_blxf(); break;
            case 3 : GameData_blxf.instance.setIsGuidanceBuild_blxf(); break;
            case 4 : GameData_blxf.instance.setIsGuidanceRoomBuild_blxf(); break;
        }

        this._guiType == 1 && UtilsModule_blxf.onReportEvent_blxf( 1, 100 );
    }

    /** 手指动画 */
    onHandAnimation_blxf () {
        let _t1 = 0.4, _t2 = 0.4;
        let _scale1 = 0.85, _scale2 = 1;
        let _tween = cc.tween( this.handNode )
        .to(_t1, { scale: _scale1 })
        .to(_t2, { scale: _scale2 })

        this.handNode.scale = 1;
        this._handTween = cc.tween( this.handNode ).repeatForever( _tween ).start();
    }

    /** 检测更新动态坐标 */
    onCheckUpdatePos_blxf () {
        if ( this._guiType == 2 ) {
            cc.game.emit('onAllShopVendorPos_blxf', ( x: number, y: number )=>{
                _guiList_2[0].x = x;
                _guiList_2[0].y = y;
            })
        }
    }

}
